<?php

namespace IDock\Insurance\TPIC\Request;

use Exception;
use IDock\Insurance\TPIC\Request\AllTPICRequest;
use IDock\Insurance\TPIC\Response\OfferOrderPriceResponse;
use IDock\Insurance\TPIC\Response\SaveVehicleResponse;
use IDock\Insurance\TPIC\Response\UnderwritingResponse;
use IDock\Insurance\TPIC\Response\VehicleModelResponse;
use IDock\Insurance\TPIC\TPICEnums;
use IDock\Visitor\Premium\PremiumCoverageVisitor;
use IDock\Visitor\Premium\PremiumInquiryVisitor;
use IDock\Visitor\Premium\PremiumUnderwritingVisitor;
use IDock\Visitor\VehicleModelVisitor;

/**
 * 核保
 */
class UnderwritingRequest extends AllTPICRequest
{
    protected $response = UnderwritingResponse::class;

    protected $urlTip = '核保';

    public $route = 'insConfirm';

    /**
     * @var PremiumInquiryVisitor $visitor
     */
    protected $visitor;

    public function generate(): array
    {
        $arr = [
            'order' => [
                'orderNo' => $this->visitor->getExtInfoVisitor()->argc['orderNo'] ?? '',
            ],
            'carOwner' => $this->getOwner(),
            'applicant' => $this->getApplicant(),
            'insured' => $this->getInsured(),
            'dispatch' => [
                'name' => $this->visitor->getOwnerVisitor()->name,
                'address' => $this->visitor->getOwnerVisitor()->address,
                'mobilePhone' => $this->visitor->getOwnerVisitor()->mobile,
            ]
        ];
        return $arr;
    }

    public function getOwner()
    {
        return [
            'name' => $this->visitor->getOwnerVisitor()->name,
            'identifyNumber' => $this->visitor->getOwnerVisitor()->certificateNo,
            'ownerNature' => $this->visitor->getOwnerVisitor()->typeCode == 'R1' ? '01' : '03',//车主性质
            'identifyType' => TPICEnums::$certificateTypeCode[$this->visitor->getOwnerVisitor()->certificateTypeCode] ?? '',//车主证件类型 默认 01 身份证
            'provinceName' => $this->visitor->getOwnerVisitor()->provinceName,
            'cityName' => $this->visitor->getOwnerVisitor()->cityName,
            'countyName' => $this->visitor->getOwnerVisitor()->areaName,
            'mobilephone' => $this->visitor->getOwnerVisitor()->mobile,
        ];
    }

    private function getApplicant()
    {
        $arr = [
            'appliName' => $this->visitor->getApplicantVisitor()->name,
            'identifyNumber' => $this->visitor->getApplicantVisitor()->certificateNo,
            'insuredType' => $this->visitor->getApplicantVisitor()->typeCode == 'R1' ? '1' : '2',//关系人类型
            'identifyType' => TPICEnums::$certificateTypeCode[$this->visitor->getApplicantVisitor()->certificateTypeCode] ?? '',//车主证件类型 默认 01 身份证
            'mobilePhone' => $this->visitor->getApplicantVisitor()->mobile,
            'email' => $this->visitor->getApplicantVisitor()->email,
            'appliAddress' => $this->visitor->getApplicantVisitor()->address,
            'identifyStartDate' => $this->visitor->getApplicantVisitor()->certificateStartDate,
            'identifyEndDate' => $this->visitor->getApplicantVisitor()->certificateEndDate,
        ];
        if(empty($arr['identifyType'])){
            throw new Exception("不支持该投保人证件类型");
        }
        if($arr['insuredType'] == '2'){
            //企业
            $arr['businessRange'] = '';//营业范围
//            $arr['isApplicantOperator'] = '1';//是否经营者
        }
        return $arr;
    }

    private function getInsured()
    {
        $arr = [
            'insuredName' => $this->visitor->getInsuredVisitor()->name,
            'identifyNumber' => $this->visitor->getInsuredVisitor()->certificateNo,
            'insuredType' => $this->visitor->getInsuredVisitor()->typeCode == 'R1' ? '1' : '2',//关系人类型
            'identifyType' => TPICEnums::$certificateTypeCode[$this->visitor->getInsuredVisitor()->certificateTypeCode] ?? '',//车主证件类型 默认 01 身份证
            'insuredMobilePhone' => $this->visitor->getInsuredVisitor()->mobile,
            'email' => $this->visitor->getInsuredVisitor()->email,
            'insuredAddress' => $this->visitor->getInsuredVisitor()->address,
            'identifyStartDate' => $this->visitor->getInsuredVisitor()->certificateStartDate,
            'identifyEndDate' => $this->visitor->getInsuredVisitor()->certificateEndDate,
        ];
        if(empty($arr['identifyType'])){
            throw new Exception("不支持该被保人证件类型");
        }
        if($arr['insuredType'] == '2'){
            $arr['businessRange'] = '';//营业范围
        }
        return $arr;
    }
}
